package com.android.stockme.frontend;

import com.android.stockme.backend.BackendController;
import com.android.stockme.backend.IBackendController;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class StockMeNewsTab extends ListActivity implements OnClickListener {

	/** 
	 * @uml.property name="newsAdapter"
	 * @uml.associationEnd multiplicity="(1 1)" inverse="stockMeNewsTab:com.android.stockme.frontend.NewsAdapter"
	 */
	private NewsAdapter newsAdapter = null;
	private IBackendController backend = null;

	private Button filterButton = null;
	private EditText symbolEntryTextField = null;	
	private ViewGroup viewContainer = null;
	private View newsView = null;
	private View newsEntryView = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.news);
		this.getListView().setEmptyView(findViewById(R.id.empty_news));
	
		this.backend = BackendController.getBackendController();
		this.newsAdapter = new NewsAdapter(this.backend, this, this);
		this.setListAdapter(this.newsAdapter);
		
		this.filterButton = (Button)findViewById(R.id.filter_symbols_button);
		this.filterButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				filterNews(v);
			}
		});
		
		this.symbolEntryTextField = (EditText)findViewById(R.id.stock_symbols);
		this.symbolEntryTextField.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
//				if (keyCode == KeyEvent.KEYCODE_ENTER) {
//					filterNews(v);
//					return true;
//				}
				return false;
			}			
		});

		viewContainer = (ViewGroup) findViewById(R.id.news_container);
		newsView = (View) findViewById(R.id.news_list_container);
		newsEntryView = (View) findViewById(R.id.news_entry_container);
	}
		
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
	}

	public void onClick(View v) {

	}
	
	private void filterNews(View v) {
		String symbolsStr = symbolEntryTextField.getText().toString().toUpperCase();
		symbolsStr = symbolsStr.replace("\n", "");
		symbolEntryTextField.setText(null);
		this.newsAdapter.setSymbolFilter(symbolsStr);
	}
}
